from datetime import datetime
from time import sleep

from  appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


class CommonDriver:
    def get_driver(self):
        desc={
            'platformName':'Android',
            'platformVersion':'7',
            'appPackage':'com.dangdang.buy2',
            # 'appPackage':'com.microvirt.launcher2',
            'appActivity':'com.dangdang.buy2.StartupActivity',
            # 'appActivity':'com.microvirt.launcher.Launcher',
            'noReset':False,
            'unicodeKeyboard':True,
            'udid':'127.0.0.1:21513',
        }

        command_executor_url='http://127.0.0.1:4723/wd/hub'

        driver=webdriver.Remote(command_executor=command_executor_url,desired_capabilities=desc)

        driver.implicitly_wait(20)
        print('连接成功')
        return driver

if __name__ == '__main__':
    driver=CommonDriver().get_driver()
    # driver.find_element(AppiumBy.ID,'com.dangdang.buy2:id/tv_sure').click()
    # driver.find_elements(AppiumBy.CLASS_NAME,'android.widget.TextView')[3].click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@text="同意"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[contains(@text,"不同")]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_dis" and @text="不同意"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.LinearLayout/android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]/parent::android.widget.LinearLayout').click()

#线性自动化，clear、quit函数
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '	//android.widget.FrameLayout[@resource-id="com.dangdang.buy2:id/home_title_layout"]/android.widget.LinearLayout').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').send_keys("浪潮之巅")
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').clear()
    # sleep(10)
    # driver.quit()

#滑动屏幕
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
    # screen_size=driver.get_window_size()
    # width=screen_size["width"]
    # height=screen_size["height"]
    # start_x=width*0.5
    # start_y=height*0.8
    # end_x=width*0.5
    # end_y=height*0.2
    # try:
    #     driver.swipe(start_x, start_y, end_x, end_y)
    # finally:
    #     driver.quit()

#根据坐标查找
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
    # driver.tap([(130,814)],500)

#长按,e.g.在桌面长按拖动图标，需先更改appPackage和appActivity
    # dangdangico=driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@text="当当"]')
    # touch=TouchAction(driver)
    # touch.long_press(dangdangico).move_to(x=79,y=660).release().perform()

#显示等待
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    print("当前日期和时间",datetime.now())
    try:
        element=WebDriverWait(driver,20).until(
            EC.visibility_of_element_located((AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sur"]'))
        )
        element.click()
    except BaseException as e:
        print(e)
    finally:
        print("当前日期和时间", datetime.now())



